<?php
	class UsersController extends Controller
	{
		public $page_title = LANG_USERS;
		public $layout = 'main';
		public $module = 'administration';
		public $helpers = array ('html', 'htmlx', 'toolbars', 'grids');
		public $javascript = array('windows');
		public $name = 'users';
		
		/**
		 * Users list
		 *
		 */
		function index()
		{
			$parameters = array('num_rows');
			$this->data[$this->name] = $this->controller_model->ListItemsPaged($this->request->get, '' ,$parameters);
			$this->data[num_rows] = $parameters[num_rows];
		}
		
		/**
		 * Adds new user
		 *
		 */
		function add()
		{
			$this->data[languages] = $this->GetModelObject('languages')->ListItems();
			if (!empty($this->request->post))
			{
				$this->request->post[password] = md5($this->request->post[password]);
				$out_parameters = array('user_adding_result');
				if ($this->controller_model->Update($this->request->post,NULL, &$out_parameters))
				{
					$user_adding_result = $out_parameters[user_adding_result];
					if($user_adding_result == 1)
					{
						$this->Redirect($this->name);
					}
					else 
					{
						if($user_adding_result == 2)
						{
							$this->controller_model->validation_errors[login] = LANG_USER_EXISTS;
						}
					}
				}
				else
				{
					$this->data[$this->name] = $this->request->post;				
				}
			}
		}
		
		/**
		 * Edits user info
		 *
		 * @param bigint $id user id
		 */
		function edit($id)
		{
			$this->data[languages] = $this->GetModelObject('languages')->ListItems();
			$this->data[themes]  = $this->ListThemes();
			$this->data[$this->name] = $this->controller_model->Get($id);			
			if (!empty($this->request->post))
			{
				if ($this->controller_model->Update($this->request->post))
				{
					//check if current user
					if ($this->data[$this->name][id] == $_SESSION[userid])
					{
						$_SESSION[usertheme] = $this->request->post[theme];
					}
					$this->Redirect($this->name);
				}
				else
				{
					$this->data[$this->name] = $this->request->post;				
				}
			}
		}

		/**
		 * Views user's info
		 *
		 * @param bigint $id user id
		 */
		function view($id)
		{
			$this->data[$this->name] = $this->controller_model->Get($id);
			$this->data[languages] = $this->GetModelObject('languages')->ListItems();
			$this->data[themes]  = $this->ListThemes();
		}
		
		/**
		 * Deletes user
		 *
		 * @param bigint $id user id
		 * @param boolean $confirm confirmation
		 */
		function delete($id, $confirm = 0)
		{ 
			if ($confirm)
			{
				$this->controller_model->Delete($id);
				$this->Redirect($this->name);
			}
			else
			{
				$this->layout = "empty";
				$this->data[$this->name][id] = $id;
			}
		}
		
		/**
		 * Edits password
		 *
		 * @param bigint $id user id
		 */
		function EditPassword($id = null)
		{
			if (empty($id)) $this->data[$this->name][id] = $_SESSION[userid];
			else $this->data[$this->name][id] = $id;
			
			if($this->IsPostBack())
			{
				$newPassword = $this->request->post[new_password];
				$retypePassword = $this->request->post[retype_password];
				if($newPassword == $retypePassword)
				{
					$result = $this->controller_model->Execute(array('id'=>$id, 'new_password'=>MD5($this->request->post[new_password])), 'sp_users_change_password_admin');
					$this->Redirect('users/edit/'.$this->data[$this->name][id]);
				}
				else
				{
					$this->controller_model->validation_errors[retype_password] = LANG_WRONG_CONFIRMATION;
				}
			}
		}
	
	}
?>